package com.example.administrator.hqq.activity.register;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.administrator.hqq.R;
import com.example.administrator.hqq.View.indicator.CirclePageIndicator;
import com.example.administrator.hqq.adapter.register.GridViewAdapter;
import com.example.administrator.hqq.adapter.register.ViewPagerAdapter;
import com.example.administrator.hqq.base.MyActivity;
import com.example.administrator.hqq.config.LoongsConfig;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 选择头像页面
 */
public class HeadPortraitActivity extends MyActivity {
    public static int item_grid_num = 20;//每一页中GridView中item的数量
    public static int number_columns = 4;//gridview一行展示的数目
    public static int index = -1;//gridview一行展示的数目
    public static int finalI1 = -1;
    private String pic_Url;
    @BindView(R.id.confirm)
    Button confirm;
    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            if (message.what == 2) {
                if (pitchView != null) {
                    pitchView.setSelected(true);
                }
            }
            return false;
        }
    });
    @BindView(R.id.app_return)
    ImageView appReturn;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.indicator)
    CirclePageIndicator indicator;
    private ViewPagerAdapter mAdapter;
    private List<Integer> dataList;
    private List<GridView> gridList = new ArrayList<>();
    private View pitchView;
    private List<String> nameList;
    private List<Integer> dataListFalse = new ArrayList<>();
    private String picName = "";
    private List<BaseAdapter> gridAdater;
    String[] strings={
            "32","33","34","35","36",
            "37","38","39","40",
            "1","2","3","4","5",
            "6","7","8","9","10",
            "11","12","13","18","19",
            "20","21","22","23","24",
            "25","26","27","28","29",
            "30","31","14","15","16",
            "17"};
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_head_portrait);
        ButterKnife.bind(this);
        initviews();
        initdata();
    }

    private void initviews() {
        nameList = new ArrayList<>();
        //初始化ViewPager
        mAdapter = new ViewPagerAdapter();
        viewPager.setAdapter(mAdapter);
        viewPager.setOffscreenPageLimit(2);
        dataList = new ArrayList<>();
        //圆点指示器
        indicator.setVisibility(View.VISIBLE);
        indicator.setViewPager(viewPager);

    }


    public int  getResourceID(String imageName){
        Context ctx=getBaseContext();
        int resId = getResources().getIdentifier(imageName, "drawable", ctx.getPackageName());
        //如果没有在"mipmap"下找到imageName,将会返回0
        return resId;
    }


    @SuppressLint("ResourceType")
    private void initdata() {
        if (dataList.size() > 0) {
            dataList.clear();
        }
        if (gridList.size() > 0) {
            gridList.clear();
        }
        for (int i = 0; i < strings.length; i++) {
            nameList.add("system_pix_v1_"+strings[i]);
            dataList.add(getResourceID("portrait_"+strings[i]));
        }
        //计算viewpager一共显示几页
        final int pageSize = dataList.size() % item_grid_num == 0
                ? dataList.size() / item_grid_num
                : dataList.size() / item_grid_num + 1;
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (pitchView != null) {
                    pitchView.setSelected(true);
                }
            }

            @Override
            public void onPageSelected(int position) {
                indicator.setCurrentItem(position);
                if (pitchView != null) {
                    pitchView.setSelected(true);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                if (state == 0) {
                    handler.sendEmptyMessage(2);
                }
                if (pitchView != null) {
                    pitchView.setSelected(true);
                }
            }
        });
        viewPager.setOffscreenPageLimit(pageSize);
        for (int i1 = 0; i1 < pageSize; i1++) {
            final GridView gridView = new GridView(this);
            gridView.setVerticalSpacing(15);
            gridView.setHorizontalSpacing(15);
            gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
            GridViewAdapter adapter = new GridViewAdapter(dataList, i1);
            gridView.setNumColumns(number_columns);
            gridView.setAdapter(adapter);
            final int finalI = i1;
            gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    if (pitchView != null&&gridList.get(finalI).getChildAt(index)!=null) {
                        pitchView.setSelected(false);
                        gridList.get(finalI).getChildAt(index).setSelected(false);
                    }
                    pitchView = view;
                    view.setSelected(true);
                    finalI1 = finalI;
                    index = i;
                    pic_Url="portrait_"+strings[finalI * 20 + i];
                    picName = nameList.get(finalI * 20 + i);
                }
            });
            gridList.add(gridView);
        }
        mAdapter.add(gridList);
    }

    @OnClick({R.id.app_return, R.id.confirm})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.app_return:
                finish();
                break;
            case R.id.confirm:
                Intent intent = getIntent();
                if (!TextUtils.isEmpty(picName))
                {
                    intent.putExtra(LoongsConfig.picName,picName);
                    intent.putExtra(LoongsConfig.pic_Url,pic_Url);
                    setResult(111,intent);
                    finish();
                }else{
                    intent.putExtra(LoongsConfig.picName,"");
                    setResult(222,intent);
                    finish();
                }
                break;
        }
    }

    @Override
    protected String getActivityName() {
        return this.getClass().getSimpleName();
    }
}
